#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "smart_timer.h"

static const char *TAG = "timer_test";

// 定时器回调函数
static void timer_callback(uint8_t timer_id, timer_action_t action, void *user_data)
{
    ESP_LOGI(TAG, "Timer %d triggered with action %d", timer_id, action);
}

// 测试单次定时器
static void test_once_timer(void)
{
    ESP_LOGI(TAG, "Testing one-time timer...");
    
    // 创建5秒后触发的单次定时器
    timer_config_t config = {
        .type = TIMER_TYPE_ONCE,
        .action = TIMER_ACTION_ON,
        .time = {
            .hour = 8,
            .minute = 0,
            .second = 5
        },
        .enabled = true
    };

    uint8_t timer_id;
    timer_err_t err = smart_timer_add(&config, &timer_id);
    if (err != TIMER_OK) {
        ESP_LOGE(TAG, "Failed to add once timer: %d", err);
        return;
    }
    ESP_LOGI(TAG, "Added once timer with id %d", timer_id);
}

// 测试每日定时器
static void test_daily_timer(void)
{
    ESP_LOGI(TAG, "Testing daily timer...");
    
    // 创建每天8:30触发的定时器
    timer_config_t config = {
        .type = TIMER_TYPE_DAILY,
        .action = TIMER_ACTION_ON,
        .time = {
            .hour = 8,
            .minute = 30,
            .second = 0
        },
        .enabled = true
    };

    uint8_t timer_id;
    timer_err_t err = smart_timer_add(&config, &timer_id);
    if (err != TIMER_OK) {
        ESP_LOGE(TAG, "Failed to add daily timer: %d", err);
        return;
    }
    ESP_LOGI(TAG, "Added daily timer with id %d", timer_id);

    // 测试更新定时器
    config.time.hour = 9;
    config.id = timer_id;
    err = smart_timer_update(&config);
    if (err != TIMER_OK) {
        ESP_LOGE(TAG, "Failed to update daily timer: %d", err);
    }
}

// 测试每周定时器
static void test_weekly_timer(void)
{
    ESP_LOGI(TAG, "Testing weekly timer...");
    
    // 创建每周一三五的定时器
    timer_config_t config = {
        .type = TIMER_TYPE_WEEKLY,
        .action = TIMER_ACTION_ON,
        .time = {
            .hour = 9,
            .minute = 0,
            .second = 0,
            .day_mask = (1 << 0) | (1 << 2) | (1 << 4)  // 周一、三、五
        },
        .enabled = true
    };

    uint8_t timer_id;
    timer_err_t err = smart_timer_add(&config, &timer_id);
    if (err != TIMER_OK) {
        ESP_LOGE(TAG, "Failed to add weekly timer: %d", err);
        return;
    }
    ESP_LOGI(TAG, "Added weekly timer with id %d", timer_id);

    // 测试禁用/启用
    err = smart_timer_set_enabled(timer_id, false);
    if (err != TIMER_OK) {
        ESP_LOGE(TAG, "Failed to disable weekly timer: %d", err);
    }

    vTaskDelay(pdMS_TO_TICKS(1000));

    err = smart_timer_set_enabled(timer_id, true);
    if (err != TIMER_OK) {
        ESP_LOGE(TAG, "Failed to enable weekly timer: %d", err);
    }
}

// 测试时间段定时器
static void test_period_timer(void)
{
    ESP_LOGI(TAG, "Testing period timer...");
    
    // 创建8:00-18:00的时间段定时器(工作日有效)
    timer_config_t config = {
        .type = TIMER_TYPE_PERIOD,
        .action = TIMER_ACTION_ON,
        .end_action = TIMER_ACTION_OFF,
        .time = {
            .hour = 8,
            .minute = 0,
            .second = 0,
            .day_mask = 0x3E  // 周一到周五
        },
        .end_time = {
            .hour = 18,
            .minute = 0,
            .second = 0
        },
        .enabled = true
    };

    uint8_t timer_id;
    timer_err_t err = smart_timer_add(&config, &timer_id);
    if (err != TIMER_OK) {
        ESP_LOGE(TAG, "Failed to add period timer: %d", err);
        return;
    }
    ESP_LOGI(TAG, "Added period timer with id %d (work days only)", timer_id);
    
    // 创建20:00-22:00的时间段定时器(每天有效)
    timer_config_t night_config = {
        .type = TIMER_TYPE_PERIOD,
        .action = TIMER_ACTION_ON,
        .end_action = TIMER_ACTION_OFF,
        .time = {
            .hour = 20,
            .minute = 0,
            .second = 0,
            .day_mask = 0  // 每天有效
        },
        .end_time = {
            .hour = 22,
            .minute = 0,
            .second = 0
        },
        .enabled = true
    };

    uint8_t night_timer_id;
    err = smart_timer_add(&night_config, &night_timer_id);
    if (err != TIMER_OK) {
        ESP_LOGE(TAG, "Failed to add night period timer: %d", err);
        return;
    }
    ESP_LOGI(TAG, "Added period timer with id %d (everyday)", night_timer_id);
    
    // 创建周末19:00-23:00的时间段定时器
    timer_config_t weekend_config = {
        .type = TIMER_TYPE_PERIOD,
        .action = TIMER_ACTION_ON,
        .end_action = TIMER_ACTION_OFF,
        .time = {
            .hour = 19,
            .minute = 0,
            .second = 0,
            .day_mask = 0x41  // 周六和周日 (位6=周六, 位0=周日)
        },
        .end_time = {
            .hour = 23,
            .minute = 0,
            .second = 0
        },
        .enabled = true
    };

    uint8_t weekend_timer_id;
    err = smart_timer_add(&weekend_config, &weekend_timer_id);
    if (err != TIMER_OK) {
        ESP_LOGE(TAG, "Failed to add weekend period timer: %d", err);
        return;
    }
    ESP_LOGI(TAG, "Added period timer with id %d (weekends only)", weekend_timer_id);
}

// 测试定时器列表操作
static void test_timer_list(void)
{
    ESP_LOGI(TAG, "Testing timer list operations...");

    // 获取定时器列表
    timer_config_t timers[32];
    uint8_t count = 32;
    timer_err_t err = smart_timer_get_list(timers, &count);
    if (err != TIMER_OK) {
        ESP_LOGE(TAG, "Failed to get timer list: %d", err);
        return;
    }
    ESP_LOGI(TAG, "Current timer count: %d", count);

    // 打印所有定时器
    for (int i = 0; i < count; i++) {
        ESP_LOGI(TAG, "Timer %d: type=%d, enabled=%d", 
                 timers[i].id, timers[i].type, timers[i].enabled);
    }

    // 删除第一个定时器
    if (count > 0) {
        err = smart_timer_delete(timers[0].id);
        if (err != TIMER_OK) {
            ESP_LOGE(TAG, "Failed to delete timer: %d", err);
        }
    }
}

// 测试调度器控制
static void test_scheduler_control(void)
{
    ESP_LOGI(TAG, "Testing scheduler control...");

    // 获取调度器状态
    bool running;
    uint8_t timer_count;
    timer_err_t err = smart_timer_get_scheduler_status(&running, &timer_count);
    if (err != TIMER_OK) {
        ESP_LOGE(TAG, "Failed to get scheduler status: %d", err);
        return;
    }
    ESP_LOGI(TAG, "Current scheduler status: running=%d, timer_count=%d", running, timer_count);

    // 暂停调度器
    err = smart_timer_pause_resume(true);
    if (err != TIMER_OK) {
        ESP_LOGE(TAG, "Failed to pause scheduler: %d", err);
    }

    vTaskDelay(pdMS_TO_TICKS(3000));  // 暂停3秒

    // 恢复调度器
    err = smart_timer_pause_resume(false);
    if (err != TIMER_OK) {
        ESP_LOGE(TAG, "Failed to resume scheduler: %d", err);
    }
}

// 主测试函数
void app_main(void)
{
    ESP_LOGI(TAG, "Starting smart timer tests...");

    vTaskDelay(pdMS_TO_TICKS(1000));

    // 初始化组件
    timer_err_t err = smart_timer_init(32);
    if (err != TIMER_OK) {
        ESP_LOGE(TAG, "Failed to init smart timer: %d", err);
        return;
    }

    // 清空所有定时器
    err = smart_timer_delete_all();
    if (err != TIMER_OK) {
        ESP_LOGE(TAG, "Failed to delete all timers: %d", err);
    }

    // 注册回调
    err = smart_timer_register_callback(timer_callback, NULL);
    if (err != TIMER_OK) {
        ESP_LOGE(TAG, "Failed to register callback: %d", err);
        return;
    }

    // 运行测试
    test_once_timer();
    test_daily_timer();
    test_weekly_timer();
    test_period_timer();
    test_timer_list();
    test_scheduler_control();

    ESP_LOGI(TAG, "All tests completed");

    // 主循环
    while (1) {
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
} 